<?php
/*
 * CRMJqueryUiTabsWidget File
 *
 * @link http://riedel-st.de
 * @author Stefan Riedel <sr@riedel-st.de>
*/

/**
 * tabs widget implements many CRMJqueryUITabWidget's
 *
 * @author Stefan Riedel
 */
class CRMJqueryUiSuperfishMenuWidget extends CRMJqueryUiBase {
	
	/**
	 * end of line e.g.: \r\n
	 * @var string
	 */
	const END_OF_LINE = "\r\n";
	
	protected $_validOptions = array ('hoverClass' => array ('type' => 'string' ), 'pathClass' => array ('type' => 'string' ), 'pathLevels' => array ('type' => 'integer' ), 'delay' => array ('type' => 'integer' ), 'animation' => array ('type' => 'array' ), 'speed' => array ('type' => 'string' ), 'autoArrows' => array ('type' => 'boolean' ), 'dropShadows' => array ('type' => 'boolean' ), 'disableHI' => array ('type' => 'boolean' ) );
	
	public $navigationArray = array ();
	
	public $supersubsOptions = array ('minWidth' => 12, 'maxWidth' => 27, 'extraWidth' => 1 );
	
	/**
	 * Widget's body
	 *
	 * @var string
	 */
	protected $_body = null;
	
	public function init() {
		if ($this->navigationArray === array ()) {
			throw new CException ( Yii::t ( 'crm', 'Leider wurden keine Navigationspunkte übergeben!' ) );
		}
	}
	
	public function registerClientsScripts() {
		parent::registerClientsScripts ();
		$superfishPath = $this->_basePath . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'jquery.ui.potato.menu.js';
		$superfishCssPath = $this->_basePath . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . $this->_cssTheme . DIRECTORY_SEPARATOR . 'jquery.ui.potato.menu.css';
		$this->_clientScript->registerScriptFile ( $superfishPath );
		$this->_clientScript->registerCssFile ( $superfishCssPath );
	}
	
	public function run() {
		list ( $name, $id ) = $this->resolveNameID ();
		$this->publishAssets ();
		$this->registerClientsScripts ();
		
		$js = $this->_jsCode ( $name );
		$this->clientScript->registerScript ( 'Yii.' . get_class ( $this ) . '#' . $id, $js, CClientScript::POS_READY );
		echo $this->_htmlCode ( $id );
	}
	
	protected function _htmlCode($id) {
		$html = '';
		foreach ( $this->navigationArray ['pages'] as $i => $nav ) {
			if (! empty ( $nav ['icon'] )) {
				$link = CRMHtml::link ( $nav ['label'], $nav ['url'], array ('class' => 'icon ' . $nav ['icon'] ) );
			} else {
				$link = CRMHtml::link ( $nav ['label'], $nav ['url'], array () );
			}
			$html .= CHtml::tag ( 'li', array (), $link, false ) . self::END_OF_LINE;
			if (! empty ( $nav ['pages'] )) {
				$html .= $this->_renderList ( $nav ['pages'] ) . self::END_OF_LINE;
			}
			$html .= CHtml::closeTag ( 'li' ) . self::END_OF_LINE;
		}
		$html = CHtml::tag ( 'ul', array ('id' => $id, 'class' => 'sf-menu' ), $html ) . self::END_OF_LINE;
		return $html;
	}
	
	protected function _renderList(array $list) {
		$html = '';
		foreach ( $list as $i => $value ) {
			if (! empty ( $value ['icon'] )) {
				$link = CRMHtml::link ( $value ['label'], $value ['url'], array ('class' => 'icon ' . $value ['icon'] ) );
			} else {
				$link = CRMHtml::link ( $value ['label'], $value ['url'], array () );
			}
			$html .= CHtml::tag ( 'li', array (), $link, false ) . self::END_OF_LINE;
			if (! empty ( $value ['pages'] )) {
				$html .= $this->_renderList ( $value ['pages'] ) . self::END_OF_LINE;
			}
			$html .= CHtml::closeTag ( 'li' ) . self::END_OF_LINE;
		}
		$html = CHtml::tag ( 'ul', array (), $html ) . self::END_OF_LINE;
		return $html;
	}
	
	protected function _jsCode($id) {
		$options = $this->_makeOptions ( $this->_options );
		$script = "$('#{$id}').ptMenu({$options});";
		return $script;
	}

}
?>
